package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcWebRtcClientMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunCameraWebRtc;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcWebRtcClient;
import com.csun.cmny.provider.model.dto.webrtc.WebRtcClientQueryDto;
import com.csun.cmny.provider.model.vo.webrtc.WebRtcClientVo;
import com.csun.cmny.provider.service.DmcCsunCameraWebRtcService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcWebRtcClientService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020/7/21 17:18
 * @Description: TODO
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcWebRtcClientServiceImpl extends BaseService<DmcWebRtcClient> implements DmcWebRtcClientService {

    @Resource
    private DmcWebRtcClientMapper dmcWebRtcClientMapper;
    @Resource
    private DmcCsunCameraWebRtcService dmcCsunCameraWebRtcService;
    @Resource
    private DmcDeviceService dmcDeviceService;

    @Override
    public List<WebRtcClientVo> queryListWithPage(WebRtcClientQueryDto webRtcClientQueryDto) {

        PageHelper.startPage(webRtcClientQueryDto.getPageNum(), webRtcClientQueryDto.getPageSize());

        return dmcCsunCameraWebRtcService.queryListWithPage(webRtcClientQueryDto);
    }

    @Override
    public Integer save(DmcWebRtcClient dmcWebRtcClient, LoginAuthDto loginAuthDto) {

        dmcWebRtcClient.setUpdateInfo(loginAuthDto);

        if (dmcWebRtcClient.isNew()) {
            return dmcWebRtcClientMapper.insertSelective(dmcWebRtcClient);
        }

        return dmcWebRtcClientMapper.updateByPrimaryKeySelective(dmcWebRtcClient);
    }

    @Override
    public Integer deleteById(Long id) {

        // 判断是否在绑定中
        DmcCsunCameraWebRtc dmcCsunCameraWebRtc = dmcCsunCameraWebRtcService.selectByKey(id);
        if (PublicUtil.isNotEmpty(dmcCsunCameraWebRtc)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100618001);
        }

        return dmcWebRtcClientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer bind(DmcCsunCameraWebRtc dmcCsunCameraWebRtc) {

        // TODO 判断客户端是否绑定
        DmcCsunCameraWebRtc record = dmcCsunCameraWebRtcService.selectByKey(dmcCsunCameraWebRtc.getClientId());
        if (PublicUtil.isNotEmpty(record)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100618001);
        }

        // TODO 判断摄像头是否绑定
        record = new DmcCsunCameraWebRtc();
        record.setDeviceId(dmcCsunCameraWebRtc.getDeviceId());
        int count = dmcCsunCameraWebRtcService.selectCount(record);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100618002);
        }

        DmcWebRtcClient dmcWebRtcClient = dmcWebRtcClientMapper.selectByPrimaryKey(dmcCsunCameraWebRtc.getClientId());
        dmcCsunCameraWebRtc.setClientNo(dmcWebRtcClient.getClientNo());

        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunCameraByDeviceId(dmcCsunCameraWebRtc.getDeviceId());
        dmcCsunCameraWebRtc.setDeviceNo(dmcDevice.getDeviceNo());

        return dmcCsunCameraWebRtcService.save(dmcCsunCameraWebRtc);
    }

    @Override
    public Integer untied(DmcCsunCameraWebRtc dmcCsunCameraWebRtc) {

        return dmcCsunCameraWebRtcService.delete(dmcCsunCameraWebRtc);
    }
}
